﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Text;
using System.Web.UI.HtmlControls;
using Enter.Entity;
using Enter.BizLogic;

namespace Enter.Web.SystemAdmin
{
    public partial class Menu : PageBase
    {
        public string menuList = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindMenuInfo();
            }
        }
        /// <summary>
        /// 绑定菜单信息
        /// </summary>
        private void BindMenuInfo()
        {
            //得到该用户的有权限的菜单
            DataTable menus = Sys_OperatorBLLSub.GetOperatorMenuList(UserInfo.LoginName);

            if (menus == null)
            {
                UserInfo = null;
                throwErrorGotoURLTop("没有任何权限查看系统", LoginPage);
                return;
            }

            //找出第一层菜单
            DataRow[] rootMenus = menus.Select("parentNo=0", "MenuOrder");

            StringBuilder innerHTML = new StringBuilder();

            for (int i = 0; i < rootMenus.Length; i++)
            {
                //判断有没有子菜单
                MenuBuilder builder = new MenuBuilder(menus, rootMenus[i], i);
                innerHTML.AppendFormat(builder.Build());
            }
            menuList = innerHTML.ToString();
            HtmlTableCell tableCell = new HtmlTableCell();
            tableCell.InnerHtml = innerHTML.ToString();
            tableCell.Attributes.Add("align", "center");
            tableCell.Attributes.Add("valign", "top");
            HtmlTableRow tableRow = new HtmlTableRow();
            tableRow.Cells.Add(tableCell);
            //  menuTable.Rows.Add(tableRow);

            menus.Dispose();
        }
    }

    class MenuBuilder
    {
        #region Variables
        private Int32 i;
        private DataTable menus;
        private Sys_PopMenuListEntity _menu = new Sys_PopMenuListEntity();

        #endregion

        #region Constructor
        public MenuBuilder(DataTable menus, DataRow rootMenus, Int32 i)
        {
            this.i = i;
            this.menus = menus;
            _menu.MenuNo = rootMenus["MenuNo"].ToString();
            _menu.MenuUrl = rootMenus["MenuUrl"].ToString();
            _menu.MenuText = rootMenus["MenuText"].ToString();
            //_menu.UrlType = (byte)rootMenus["UrlType"];
        }

        private String GetClickStr()
        {
            if (String.IsNullOrEmpty(_menu.MenuUrl)) return "";

            //if (_menu.IsJsUrl) return _menu.MenuUrl;

            if (_menu.MenuUrl.StartsWith("/")) return "goMenuUrl('" + _menu.MenuUrl + "')>";

            return "goMenuUrl('" + _menu.MenuUrl + "')";
        }
        #endregion

        #region Public Method
        public String Build()
        {
            StringBuilder innerHTML = new StringBuilder();
            DataRow[] childMenus = menus.Select(String.Format("parentNo='{0}'", _menu.MenuNo), "MenuOrder");

            innerHTML.AppendFormat(@"<li><a class='head'>{0}</a>
            </li>{1}", _menu.MenuText, BuildSubMenu());



            return innerHTML.ToString();
        }

        private String BuildSubMenu()
        {
            return String.Format(
                @"<ul>{0}</ul>"
                , BindChildMenuInfo(_menu.MenuNo));
        }


        /// <summary>
        /// 绑定子菜单信息
        /// </summary>
        private string BindChildMenuInfo(string menuNo)
        {
            DataRow[] subMenus = menus.Select(String.Format("parentNo='{0}'", menuNo), "menuOrder");
            if (subMenus.Length == 0) return "";

            StringBuilder html = new StringBuilder();

            foreach (DataRow row in subMenus)
            {
                Sys_PopMenuListEntity subMenu = new Sys_PopMenuListEntity();
                subMenu.MenuNo = row["MenuNo"].ToString();
                subMenu.MenuUrl = row["MenuUrl"].ToString();
                subMenu.MenuText = row["MenuText"].ToString();


                html.AppendFormat(
                    @"<li><a href='{0}' target='rightFrame'>{1}</a></li>"
                  , subMenu.MenuUrl
                    , subMenu.MenuText
                    );

                html.Append(BindChildMenuInfo(subMenu.MenuNo));
            }

            return html.ToString();
        }





        #endregion

    }
}